<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>

<?php	

	include("inc/joomlaInt.php");
	include("inc/inc_configuration.php");
	
if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{	
	$debug=0;
	
	$tTid=$_POST["team"];
	
	if ($tTid == "")
	{
		echo "Bitte ein Team auswählen!";
		break;
	}
	$anz=$_POST["anz"];
	
	$q = "SELECT t.name,t.rating,lt.leagueId, t.id AS id FROM $tbl_lt lt, $tbl_teams t WHERE lt.teamId=t.id AND t.id=$tTid";
	$qe = mysql_query($q);
	$qeo = mysql_fetch_object($qe);
	
	
	$tTr = $qeo->rating;
	$tTl = $qeo->leagueId;
	$tTn = $qeo->name;
	$tTid = $qeo->id;
	
	
	$af_t_vh="	SELECT l.Id AS leagueId,  t.id AS teamId, t.rating AS rating, t.name AS name, c.id AS cId  
			FROM $tbl_combinations c, $tbl_teams t, $tbl_leagues l, $tbl_season s, $tbl_lt lt 
			WHERE 	c.userID=$jUser AND 
					c.active=1 AND 
					c.teamId=t.id AND
					s.active=1 AND
					l.seasonId=s.id AND
					lt.teamId=t.id AND
					lt.leagueId=l.id AND
					l.type=1"; //vorhandene Teams suchen
	$e_t_vh = mysql_query($af_t_vh);
	$count = mySQL_num_rows($e_t_vh);
	
	
	
	
	
	if ($count>=1)
	{
		$o=mysql_fetch_object($e_t_vh);
		$t1id=$o->teamId;
		$t1r=$o->rating;
		$t1l=$o->leagueId;
		$t1n=$o->name;
		$t1c=$o->cId;
	}
	if ($count==2)
	{
		$o=mysql_fetch_object($e_t_vh);
		$t2id=$o->teamId;
		$t2r=$o->rating;
		$t2l=$o->leagueId;
		$t2n=$o->name;
		$t2c=$o->cId;
	}
	
	// echo "C: $count";
	
	if ($count==1)
	{
		$p=1;
	}
	else
	{
			$t1la=0;
			$t2la=0;
			$t1ra=0;
			$t2ra=0;
			$t1a=0;
			$t2a=0;
			
			if ($tTl==$t1l)
			{
				$t1la=1;
			}
			
			if ($tTl==$t2l)
			{
				$t2la=1;
			}
			
			if (($maxrating-$t1r)<$tTr)
			{
				$t1ra=1;
			}
			if (($maxrating-$t2r)<$tTr)
			{
				$t2ra=1;
			}
			
			
			
			if (($t1la==1) OR ($t1ra==1))
			{
				$t1a=1;
			}
			
			if (($t2la==1) OR ($t2ra==1))
			{
				$t2a=1;
			}
			
			
			
			$p=3;
			
			
			if ($t1a==1)
			{
				$p=1;	//Team 1 muss abgegeben werden, es sei denn..
				
				if ($t2a==1)
				{
					$p=2;	//Team 1 & 2 müssen abgegeben werden
					
					if (($t1ra==1) AND ($t2ra==1) AND ($t1la==0) AND ($t2la==0))
					{
						$p=3; //Team 1 ODER 2 
					}
				}
			}
			else
			{
				if (($t1a==0) AND ($t2a==1))
				{
					$p=4; //Team 2 muss abgegeben werden
					
					if (($t1ra==1) AND ($t2ra==1) AND ($t1la==0) AND ($t2la==0))			
					{
						$p=3; //Team 1 ODER 2 
					}
				}
			}
			
			if ($debug==1)
			{
				echo "HALLO: $p";
				echo "Team1: $t1n, $t1r Sterne, Liga: $t1l - Team2: $t2n, $t2r Sterne, Liga: $t2l<br><br><br>Tauschteam: ID:$tTid $tTr Sterne, Liga: $tTl";
				echo "<br><br>t1r:$t1ra<br>t1l:$t1la<br>t1a:$t1a<br><br>t2r:$t2ra<br>t2l:$t2la<br>t2a:$t2a<br><br>";
			}
	}
	
	$qs = "SELECT * FROM $tbl_season s WHERE s.active=1";
	$qse = mysql_query($qs);
	$qseo = mysql_fetch_object($qse);
	$season = $qseo->id;
	
	$datetime = time();
	
	$i=0;
	
	switch($p)
	{
		case (1):
		{
			$queries = array();
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t1id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tTid";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t1c";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasoniD) VALUES ($t1c,2,$datetime,'Teamtausch',$season)";
			
			$a = 		"
							<td>
								<center>Sie müssen folgendes Team abgeben: $t1n</center>
							</td>
						</tr>
						<tr>
							<td>
								<center>Wollen sie dieses Team wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								<form action='teamtausch_v2.php' method='post'>
								<input type='text' hidden name='query' value='$q'>
								<input type='text' hidden name='team' value='$tTid'>
								<input type='text' hidden name='type' value='1'>
								<input type='text' hidden name='season' value='$season'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
					$q=$queries;

		}
		break;
		
		
		case (2):
		{
			$queries = array();
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t1id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t2id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tTid";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t1c";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t2c";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($t1c,2,$datetime,'Teamtausch',$season)";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($t2c,2,$datetime,'Teamtausch',$season)";
			
			// $a = 		"Sie müssen folgende Teams abgeben: $t1n und $t2n <br>
						// Wollen sie diese Teams wirklich für ihr neues Team $tTn abgeben?
						// <form action='teamtausch_v2.php' method='post'>
						// <input type='text' hidden name='query' value='$q'>
						// <input type='text' hidden name='team' value='$tTid'>
						// <input type='text' hidden name='type' value='1'>
						// <input type='text' hidden name='season' value='$season'>
						// <input type='submit' value='ja'>
						// </form>
									
					// ";
					
					$a = 		"
							<td>
								<center>Sie müssen folgende Teams abgeben: $t1n und $t2n</center>
							</td>
						</tr>
						<tr>
							<td>
								<center>Wollen sie diese Teams wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								<form action='teamtausch_v2.php' method='post'>
								<input type='text' hidden name='query' value='$q'>
								<input type='text' hidden name='team' value='$tTid'>
								<input type='text' hidden name='type' value='1'>
								<input type='text' hidden name='season' value='$season'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
			$q=$queries;
			
		}
		break;
		
		case (3):
		{
			$queries = array();
			$queries[] = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tTid";
			
			// $a = 		"Sie müssen eins der folgenden Teams abgeben:<br>
						// <form action='teamtausch_v2.php' method='post'>
						// <input type='radio' name='team' value='$t1id'>$t1n<br>
						// <input type='radio' name='team' value='$t2id'>$t2n<br>
						// <input type='text' name='teamN' hidden value='$tTid'>
						
						
						// <input type='text' hidden name='type' value='2'>
						// <input type='text' hidden name='query' value='$q'>
						// <input type='text' hidden name='season' value='$season'>
						// Wollen sie dieses Team wirklich für ihr neues Team $tTn abgeben?<br>
						// <input type='submit' value='ja'>
						// </form>
									
					// ";
					
					$a = 		"
							<td>
								<form action='teamtausch_v2.php' method='post'>
								<center>Sie müssen eins der folgenden Teams abgeben:</center>
							</td>
						</tr>
						<tr>
							<td><input type='radio' name='team' value='$t1id'>$t1n</td>
							<td><input type='radio' name='team' value='$t2id'>$t2n</td>
							
						<tr>
							<td>
								<center>Wollen sie diese Teams wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								
								<input type='text' name='teamN' hidden value='$tTid'>
								<input type='text' hidden name='type' value='2'>
								<input type='text' hidden name='query' value='$q'>
								<input type='text' hidden name='season' value='$season'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
			$q=$queries;
			
		}
		break;
		
		case (4):
		{	
			$queries = array();
			$queries[] = "UPDATE $tbl_teams SET vergeben=0 WHERE id=$t2id";
			$queries[] = "UPDATE $tbl_teams SET vergeben=1 WHERE id=$tTid";
			$queries[] = "UPDATE $tbl_combinations SET active=0 WHERE id=$t2c";
			$queries[] = "INSERT INTO $tbl_trainerwechsel (combinationId,type,date,reason,seasonId) VALUES ($t2c,2,$datetime,'Teamtausch',$season)";
			
			// $a = 		"Sie müssen folgendes Team abgeben: $t2n <br>
						// Wollen sie dieses Team wirklich für ihr neues Team $tTn abgeben?
						// <form action='teamtausch_v2.php' method='post'>
						
						// <input type='text' hidden name='team' value='$tTid'>
						// <input type='text' hidden name='type' value='1'>
						// <input type='text' hidden name='season' value='$season'>
						// <input type='submit' value='ja'>
						// </form>
									
					// ";				
					
					$a = 		"
							<td>
								<center>Sie müssen folgendes Team abgeben: $t2n</center>
							</td>
						</tr>
						<tr>
							<td>
								<center>Wollen sie dieses Team wirklich für ihr neues Team $tTn abgeben?</center>
							</td>
						</tr>
						<tr>
							<td>
								<form action='teamtausch_v2.php' method='post'>
								<input type='text' hidden name='team' value='$tTid'>
								<input type='text' hidden name='type' value='1'>
								<input type='text' hidden name='season' value='$season'>
								<input type='submit' class='button eintragen' value='Ja'>
							</td>
						<td>
							<a href='javascript:window.history.back()' class='button zurueck'>Nein</a>
						</td>
						
						</form>
					";
					
					$q=$queries;
					
		}
		break;
	}
	
	echo	"	<table>
					<thead id='tabelle'>
						<tr>
							<th colspan='2'></th>
						</tr>
					</thead>
					<tr>
						$a
					</tr>
					<thead id='tabelle'>
						<tr>
							<th colspan='2'></th>
						</tr>
					</thead>
				</table>";
	$_SESSION['tquery']=$q;
}
?>

</body>
</html>